from flask import Blueprint, request, jsonify
from common.libs.translation.baidu_translation import  BaiduTranslation
from application import app,db
router_translation = Blueprint("translation", __name__)
@router_translation.route("/search", methods = ['POST', 'GET'])
def search():
    resp = {'code': 200, 'msg': "获取成功", "data":{}}
    req = request.values
    q = req['q'] if 'q' in req else ""
    if q is None:
        resp['code'] = -1
        resp['msg'] = "查询失败"
        return jsonify(resp)
    res = BaiduTranslation().search(q)
    # 做了一层容错处理
    if res['code'] == -1:
        res = BaiduTranslation().search(q)
        #print(q)

    if res['code'] == -1:
        resp['code'] = -1
        resp['msg'] = "数据获取失败"
        return jsonify(resp)

    resp['data'] = res['data']
    return jsonify(resp)



